﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// MTDSalesByPlan needs a STATE col, a PROP_TRAC_NAME, TRAC_PROP_NAME, a GM col
/// </summary>
public class MTDSalesByPlan
{

    private DataView _dvMTDSalesByPlan;
    public MTDSalesByPlan()
    {

        //_dvMTDSalesByPlan = ((System.Data.DataSet)HttpContext.Current.Cache["ACC_MARKET_PENETRATION"]).Tables[0].DefaultView;

        //if (!_dvMTDSalesByPlan.Table.Columns.Contains("PROPERTY"))
        //{
        //    _dvMTDSalesByPlan.Table.Columns.Add("PROPERTY");
        //}

        //if (((RollupMetadata)HttpContext.Current.Session["RollupMetadata"]).ByTracId)
        //{
        //    _dvMTDSalesByPlan.Sort = "TRAC_ID";
        //    _dvMTDSalesByPlan.Table.Columns["PROPERTY"].Expression = "TRIM(TRAC_ID) + ' - ' + PROPERTY_NM";
        //}
        //else
        //{
        //    _dvMTDSalesByPlan.Sort = "PROPERTY_NM";
        //    _dvMTDSalesByPlan.Table.Columns["PROPERTY"].Expression = "PROPERTY_NM +' (' + TRIM(TRAC_ID) + ')'";
        //}
    }

    public MTDSalesByPlan(string psDud)
    {
    }

    public DataView Select(string psWhere)
    {
        string sTable = psWhere.Split('=')[0];
        _dvMTDSalesByPlan = ((System.Data.DataSet)HttpContext.Current.Cache["MTD_SALES_ROLLUPS"]).Tables[sTable].DefaultView;
        //if (!_dvMTDSalesByPlan.Table.Columns.Contains("PROPERTY_NM"))
        //{
        //    _dvMTDSalesByPlan.Table.Columns.Add("PROPERTY_NM");
        //    _dvMTDSalesByPlan.Table.Columns["PROPERTY_NM"].Expression = "";
        //}
        DataView dv = _dvMTDSalesByPlan;
        dv.RowFilter = psWhere;
        return dv;

    }

    private DataView Select(string psWhere, string psOrderBy)
    {
        DataView dv = Select(psWhere);
        dv.Sort = psOrderBy;
        return dv;
    }

    public DataView ByAreaMgr(string psAreaMgr)
    {
        return Select("AREA_MGR='" + psAreaMgr.Replace("'", "''") + "'");
    }

    public DataView ByAcctMgr(string psAcctMgr)
    {
        return Select("ACCT_MGR='" + psAcctMgr.Replace("'", "''") + "'");
    }

    public DataView ByMarket(string psMarket)
    {
        return Select("MARKET='" + psMarket + "'");
    }

    public DataView ByState(string psStateCD)
    {
        if (psStateCD== "ZZ")
            return Select("STATE_CD LIKE'%'");
        else
            //CREATE A STATE COL FROM TRAC ID OR USE SUBSTR?
            return Select("STATE_CD='" + psStateCD + "'");
    }

    public DataView ByPortfolio(string psPortfolio)
    {
        return Select("PORTFOLIO='" + psPortfolio.Replace("'", "''") + "'");
    }

    public DataView ByTRAC_ID(string psProperty)
    {
        return SQLServer.GetMTDSalesDataTable("TRAC_ID", psProperty, (new Caching().CacheDate)).DefaultView;
        //return Select("TRAC_ID='" + psProperty.Replace("'", "''") + "'");
    }


    public DataView All()
    {
        return _dvMTDSalesByPlan;
    }

    public DataView ByRegion(string psRegion)
    {
        //JOIN GM TO YOUR GLOBAL DV
        return SQLServer.GetMTDSalesDataTable("REGION", psRegion,(new Caching()).TeradataDate).DefaultView;
        //return Select("REGION='" + .Replace("'", "''") + "'");
    }

    public DataView ByGenMgr(string psGenMgr)
    {
        //JOIN GM TO YOUR GLOBAL DV
        return Select("GEN_MGR='" + psGenMgr.Replace("'", "''") + "'");
    }


}